package manuscript.copulamodels;

import java.io.IOException;

import repicea.math.Matrix;
import repicea.stats.ParameterBound;
import repicea.stats.data.DataSet;
import repicea.stats.data.StatisticalDataException;
import repicea.stats.model.glm.GeneralizedLinearModel;
import repicea.stats.model.glm.HosmerLemeshowTest;
import repicea.stats.model.glm.LinkFunction.Type;
import repicea.stats.model.glm.copula.CopulaExpression;
import repicea.stats.model.glm.copula.CopulaLibrary;
import repicea.stats.model.glm.copula.FGMCopulaGLModel;

public class CopulaModelExample {

	private GeneralizedLinearModel glm;
//	private FGMCopulaGLModel copulaSimpleModel;
	private FGMCopulaGLModel copulaDistanceModel;
	
	
	protected CopulaModelExample(String modelDefinition, String suffixe) {
		String filename = "D:/Travail/3_Publications/1_Manuscrits/4_En preparation/2013 - DPothierAdaptationMaitriseSimon/Analyses/donneesFinales.csv";
		String outputFilename = "D:/Travail/3_Publications/1_Manuscrits/4_En preparation/2013 - DPothierAdaptationMaitriseSimon/Analyses/simul" + suffixe + ".csv";
		DataSet dataSet = new DataSet(filename);
		
		
		glm = new GeneralizedLinearModel(dataSet, Type.Logit, modelDefinition);
		glm.optimize();
//		System.out.println(new HosmerLemeshowTest(glm).toString());
		
		try {

//			CopulaExpression simpleCopula = new CopulaLibrary.SimpleCopulaExpression(0.2, "IDENT"); 
//			copulaSimpleModel = new FGMCopulaGLModel(glm, simpleCopula);
//			copulaSimpleModel.gridSearch(copulaSimpleModel.getParameters().m_iRows - 1, .05d, .25d, .01);
//			copulaSimpleModel.optimize();
			
			CopulaExpression distanceCopula = new CopulaLibrary.DistanceLinkFunctionCopulaExpression(Type.Log, "IDENT", "X + Y", -.15);		// no intercept in the linear expression
			distanceCopula.setBounds(0, new ParameterBound(null, 0d));
			copulaDistanceModel = new FGMCopulaGLModel(glm, distanceCopula);
//			copulaDistanceModel.setConvergenceCriterion(1E-3);
			copulaDistanceModel.gridSearch(copulaDistanceModel.getParameters().m_iRows - 1, -.25d, -.15d, .01);
			copulaDistanceModel.optimize();

//			glm.getSummary();
//			copulaSimpleModel.getSummary();
			copulaDistanceModel.getSummary();
			System.out.println(new HosmerLemeshowTest(copulaDistanceModel).toString());

			Matrix test = copulaDistanceModel.getLinearPredictions();
			DataSet dataset = copulaDistanceModel.getDataStructure().getDataSet();
			Object[] field1 = new Object[test.m_iRows];
			Object[] field2 = new Object[test.m_iRows];
			for (int i = 0; i < test.m_iRows; i++) {
				field1[i] = test.m_afData[i][0];
				field2[i] = test.m_afData[i][1];
			}
			dataset.addField("linPred", field1);
			dataset.addField("linPredVar", field2);
			
			dataset.save(outputFilename);
			
		} catch (StatisticalDataException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@SuppressWarnings("unused")
	public static void main(String args[]) {
		String simpleDefinition = "coupe ~ essence + diffdhp + marchand:diffdhp + marchand:diffdhp2";
		CopulaModelExample example1 = new CopulaModelExample(simpleDefinition, "Gen");
		
		String complexDefinition2 = "coupe ~ essence + diffdhp + marchand:diffdhp + marchand:diffdhp2 + marchand:MSCR_ID#R";
		CopulaModelExample example2 = new CopulaModelExample(complexDefinition2, "MSCR");

		String complexDefinition3 = "coupe ~ essence + diffdhp + marchand:diffdhp + marchand:diffdhp2 + marchand:test#A";
		CopulaModelExample example3 = new CopulaModelExample(complexDefinition3, "ABCD");
		
		String complexDefinition4 = "coupe ~ essence + diffdhp + marchand:diffdhp + marchand:diffdhp2 + marchand:hvq2#v1";
		CopulaModelExample example4 = new CopulaModelExample(complexDefinition4, "1234");

	}

	
	
	
	
}
